package top.scsoul.voes.web.controller.examination;

import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.scsoul.voes.common.core.domain.AjaxResult;
import top.scsoul.voes.common.core.domain.model.IdsVo;
import top.scsoul.voes.examination.entity.QuestionBank;
import top.scsoul.voes.examination.service.QuestionBankService;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/questionBank")
public class QuestionBankController {

    @Autowired
    QuestionBankService questionBankService;


    @RequestMapping("/questionBanks")
    public AjaxResult list(
            QuestionBank questionBank,
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<QuestionBank> questionBankPageInfo = questionBankService.queryAll(questionBank, currentPage, pageSize);
        return AjaxResult.success(questionBankPageInfo);
    }

    @RequestMapping("/get/{id}")
    public AjaxResult list(@PathVariable int id) {
        QuestionBank questionBank = questionBankService.queryById(id);
        return AjaxResult.success(questionBank);
    }

    //    下面的记得添加权限
    @RequestMapping("/add")
    public AjaxResult add(@Valid @RequestBody QuestionBank questionBank) {
        QuestionBank insert = questionBankService.insert(questionBank);
        return AjaxResult.success(insert);
    }

    @RequestMapping("/update")
    public AjaxResult update(@Valid @RequestBody QuestionBank questionBank) {
        QuestionBank insert = questionBankService.update(questionBank);
        return AjaxResult.success(insert);
    }

    @RequestMapping("/delete")
    public AjaxResult delete(Integer id) {
        boolean flag = questionBankService.deleteById(id);
        return AjaxResult.success(flag);
    }

    @RequestMapping("/deletes")
    public AjaxResult deletes(IdsVo idsVo) {
        List<Integer> ids = idsVo.getIds();
        questionBankService.deleteByIds(ids);
        return AjaxResult.success();
    }

    @RequestMapping("/searchCount")
    public AjaxResult searchCount(String keyWord) {
        Map<String, Long> map = questionBankService.searchCount(keyWord);
        return AjaxResult.success(map);
    }


    @RequestMapping("/getImportExcel")
    public AjaxResult importExcelDownload(HttpServletResponse response) throws Exception {
       questionBankService.getExcelModel(response);
       return AjaxResult.success();
    }

    @RequestMapping("/import")
    public AjaxResult importExcelUpload(MultipartFile file) throws Exception {
        Integer success = questionBankService.uploadExcel(file);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("success",success);
        return AjaxResult.success(map);

    }



}
